-- NewsPaperRender31
-- 对应样式 3*1

local NewTeamMemberHeadRender = require("app.team.views.renders.NewTeamMemberHeadRender")
local UserBaseInfo = require("app.public.dao.UserBaseInfo")

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewsPaperRender_31))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.text:setVisible(false)
    self._ui.title:setVisible(false)

    self._headIcon = NewTeamMemberHeadRender.create( self._ui["Panel/text/character"] )

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local eventId = data.eventId
        local playerInfo = data.newsInfo or {}
        local extraStr = (playerInfo[1] and playerInfo[1].BaseInfo) and playerInfo[1].BaseInfo.name or "" -- 目前只需要玩家名字
        local conf = GD:getNewsPaperEventById(eventId)
        if not conf or not conf.ui31 then return end
        if conf.ui31 == "text" then
            self._ui.text:setVisible(true)
            self:initTextNode()
            local imageNode11 = self.imageNode11
            local imageNode11c = self.imageNode11c
            local imageNode = imageNode11
            local tltNode = self._ui["Panel/text/tlt_1"]
            if tonumber(conf.player) == 1 then -- 有人物图片 (标题宽度缩短)
                imageNode = imageNode11c
                if playerInfo[1] and playerInfo[1].BaseInfo then
                    local baseInfo = UserBaseInfo.new(playerInfo[1].BaseInfo)
                    self._headIcon:setData(baseInfo)
                    self._headIcon:setLvVisible(false)
                    self._headIcon:onClick(function(sender)
                        if baseInfo.uid ~= UD.player.uid then --点击别人的头像
                            local size = sender:getPanelSize()
                            local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
                            UIHandler:showHeadMenu({online=true, attr=baseInfo, pos=pos})
                        end
                    end)
                end
                self._ui["Panel/text/bg"]:setVisible(true)
            end           
            local titleStr = conf.describe and "    "..conf.title1 or ""..conf.title1
            tltNode:setString(string.format(L(titleStr), extraStr))               
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic31)
            imageNode:setVisible(true)
            imageNode:loadTexture(bgPath)
            if conf.describe then
                self._ui.Button_Check:setVisible(true)
                self._ui.Button_Check:setTouchEnabled(true)
                self._ui.Button_Check:addTouchEventListener(function(sender, eventType)
                    if eventType == ccui.TouchEventType.ended then
                        executeFunc(self._onOperate, "openDetailsView", data)
                    end
                end)
            end
        elseif conf.ui31 == "title" then
            self._ui.title:setVisible(true)
            self._ui["Panel/title/tlt_1"]:setString(string.format(L(conf.title1), extraStr))
            self._ui["Panel/title/tlt_2"]:setString(string.format(L(conf.title2), extraStr))
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic31)
            self._ui["Panel/title/bg"]:loadTexture(bgPath)
        end
    end
end

function ClassRef:initTextNode()
    self.imageNode11 = self._ui["Panel/text/Image_11"]
    self.imageNode11c = self._ui["Panel/text/Image_11c"]
    self.imageNode11:setVisible(false)
    self.imageNode11c:setVisible(false)
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef